import { PlusOutlined } from '@ant-design/icons';
import { Button, message, Input, Drawer } from 'antd';
import React, { useState, useRef } from 'react';
import { PageContainer, FooterToolbar } from '@ant-design/pro-layout';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { ModalForm, ProFormDatePicker, ProFormDateTimePicker, ProFormText, ProFormTextArea, ProFormTimePicker } from '@ant-design/pro-form';
import type { ProDescriptionsItemProps } from '@ant-design/pro-descriptions';
import ProDescriptions from '@ant-design/pro-descriptions';
import type { FormValueType } from './components/UpdateForm';
import UpdateForm from './components/UpdateForm';
import { getArticleType,updateArticleType,rule, addRule, updateRule, removeRule,addArticleType} from './service';
import type { TableListItem, TableListPagination } from './data';
import { setLocale } from 'umi';
import { getArticleCheck} from '../article-list/service';
import { values } from 'lodash';



const TableList: React.FC = () => {
  /** 新建窗口的弹窗 */ 
  const [createModalVisible, handleModalVisible] = useState<boolean>(false);
  /** 分布更新窗口的弹窗 */
  const [updateModalVisible, handleUpdateModalVisible] = useState<boolean>(false);
  const [showDetail, setShowDetail] = useState<boolean>(false);
  const actionRef = useRef<ActionType>();
  const [currentRow, setCurrentRow] = useState<TableListItem>();
  const [selectedRowsState, setSelectedRows] = useState<TableListItem[]>([]);
  // 新增删除接口
  const workType = (param:number,check:string)=>{
    getArticleCheck({id:param,check:check})
  }
  const deleteArticleType = async(value: any)=>{
    const {code} = await updateArticleType({type:value,is_delete:1})
    handleModalVisible(false);
    if (actionRef.current) {
      actionRef.current.reload();
    }
    code === 1000 ? message.success('该类型删除成功') : message.error('该类型删除失败')
  }
  const handleAddArticleType = async(param: Record<string, any>)=>{
    const {code} = await addArticleType({type:param.type})
    handleModalVisible(false);
      if (actionRef.current) {
      actionRef.current.reload();
    }
    code === 1000 ? message.success('该类型添加成功') : message.error('该类型添加失败')
  }
  /** 国际化配置 */
  // 列的内容
  const columns: ProColumns<TableListItem>[] = [
    {
      title: 'id',
      dataIndex: 'id',
      search: false,
      // tip: '规则名称是唯一的 key',
      render: (dom, entity) => {
        return (
          <a
            onClick={() => {
              setCurrentRow(entity);
              setShowDetail(true);
              console.log(dom, entity)
            }}
          >
            {dom}
          </a>
        );
      },
    },
    {
      title: '文章类型',
      dataIndex: 'type',
      valueType: 'textarea',
      hideInForm: true,

    },
    {
      title: '创建时间',
      dataIndex: 'createdAt',
      valueType: 'dateRange',
      search: {
        transform: (value: any) => ({ createdFrom: value[0], createdTo: value[1] }),
      },
      render: (dom, record) => {
        return (
          <span>{record.createdAt}</span>
        );
      },
    },
    {
      title: '更新时间',
      dataIndex: 'updatedAt',
      valueType: 'dateRange',
      search: false,
      render: (dom, record) => {
        return (
          <span>{record.updatedAt}</span>
        );
      },
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => [
        <a
          key="config"
          onClick={() => {
            deleteArticleType(record['type'])
            setCurrentRow(record);
          }}
        >
          删除
        </a>

      ],
    },
  ];

  return (
    <PageContainer>
      <ProTable<TableListItem, TableListPagination>
        headerTitle="文章分类"
        actionRef={actionRef}
        rowKey="id"
        search={{
          labelWidth: 120,
        }}
        toolBarRender={() => [
          <Button
            type="primary"
            key="primary"
            onClick={() => {
              handleModalVisible(true);
            }}
          >
            <PlusOutlined /> 添加
          </Button>,
        ]}
        // 请求列表数据接口
        request={getArticleType}
        columns={columns}
        rowSelection={{
          onChange: (_, selectedRows) => {
            setSelectedRows(selectedRows);
          },
        }}
      />
      {selectedRowsState?.length > 0 && (
        <FooterToolbar
          extra={
            <div>
              已选择{' '}
              <a
                style={{
                  fontWeight: 600,
                }}
              >
                {selectedRowsState.length}
              </a>{' '}
              项 &nbsp;&nbsp;
              <span>
                服务调用次数总计 {selectedRowsState.reduce((pre, item) => pre + item.callNo!, 0)} 万
              </span>
            </div>
          }
        >
          {/* <Button
            onClick={async () => {
              await handleRemove(selectedRowsState);
              setSelectedRows([]);
              actionRef.current?.reloadAndRest?.();
            }}
          >
            批量删除
          </Button>
          <Button type="primary">批量审批</Button> */}
        </FooterToolbar>
      )}
      <ModalForm
        title="新增文章类型"
        width="400px"
        visible={createModalVisible}
        onVisibleChange={handleModalVisible}
        onFinish={async (value) => {
          value.is_delete = 0
          console.log('我点击了新增',value)

          handleAddArticleType(value)
        }}
      >
        文章类型
        <ProFormText
          rules={[
            {
              required: true,
              message: '规则名称为必填项',
            },
          ]}
          width="md"
          name="type"
        />
      </ModalForm>

 
    </PageContainer>
  );
};

export default TableList;
